This example uses the Palmer Penguins data set: https://github.com/allisonhorst/palmerpenguins.
Palmer Penguins Illustration from @allison_horst
library(palmerpenguins) # penguin data
library(pander) # nicely formatted tables
pander(head(penguins)) # nicely formatted table of top of data| species | island | culmen_length_mm | culmen_depth_mm | flipper_length_mm |
|---|---|---|---|---|
| Adelie | Torgersen | 39.1 | 18.7 | 181 |
| Adelie | Torgersen | 39.5 | 17.4 | 186 |
| Adelie | Torgersen | 40.3 | 18 | 195 |
| Adelie | Torgersen | NA | NA | NA |
| Adelie | Torgersen | 36.7 | 19.3 | 193 |
| Adelie | Torgersen | 39.3 | 20.6 | 190 |
| body_mass_g | sex |
|---|---|
| 3750 | MALE |
| 3800 | FEMALE |
| 3250 | FEMALE |
| NA | NA |
| 3450 | FEMALE |
| 3650 | MALE |
We calculate the correlation of body mass and flipper length.
We need to use the option
use = "complete.obs"to avoid an error message because some observations have missing data.
## [1] 0.8712018
There is some indication that penguins with higher body mass have longer flippers.
To get a more nicely formatted correlation value, we can read this correlation into a variable, and then print out this correlation as part of a sentence in inline code. See this RMarkdown document for how this is done, or take a look at this page from RStudio.
The value of the correlation is 0.8712018.
Gorman KB, Williams TD, Fraser WR (2014). Ecological Sexual Dimorphism and Environmental Variability within a Community of Antarctic Penguins (Genus Pygoscelis). PLoS ONE 9(3): e90081. https://doi.org/10.1371/journal.pone.009008